package com.xxiongb.business.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

	
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		if(!"yiyi".equals(username)){
			throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
		}
		
		return new MyUserDetails();
	}
	
	public static List<GrantedAuthority> getGrantedAuthorities(String... roles) {
	    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(2);
	    for (String role : roles) {
	        authorities.add(new SimpleGrantedAuthority(role));
	    }
	    return authorities;
	} 
	
	
	private final static class MyUserDetails implements UserDetails{

		private static final long serialVersionUID = 8063650168253495165L;

		@Override
		public Collection<? extends GrantedAuthority> getAuthorities() {
			return getGrantedAuthorities("USER");
		}

		@Override
		public String getPassword() {
			
			return "123456";
		}

		@Override
		public String getUsername() {
			// TODO Auto-generated method stub
			return "yiyi";
		}

		@Override
		public boolean isAccountNonExpired() {
			return true;
		}

		@Override
		public boolean isAccountNonLocked() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean isCredentialsNonExpired() {
			return true;
		}

		@Override
		public boolean isEnabled() {
			return true;
		}
		
	}

}
